package com.xiaobo.framework.api;

import com.xiaobo.framework.bean.Message;
import com.xiaobo.framework.service.OrdinarySender;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-05-18 15:14
 **/
@RestController
@RequestMapping("/message")
@Api(tags = "消息模板")
@Slf4j
public class MessageApi {

    @Autowired
    private OrdinarySender ordinarySender;


    @ApiOperation(value = "通讯录联系人", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = Message.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @PostMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> send(@ApiParam(value = "消息模板") @RequestBody Message message) {
        try {
            Optional.ofNullable(message).orElseThrow(() -> new RuntimeException("消息内容为空"));
            this.ordinarySender.send(message);
        } catch (Exception ex) {
            log.error("消息错误",ex);
        }
        return ResponseEntity.ok().build();
    }




}
